import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import VueCookies from 'vue-cookies'
const Cookies: any = VueCookies


import Home from '../views/home.vue'
import Login from '../views/login.vue'
import NotFound from '../views/404.vue'

import nepmRouter from './nepm';//管理员
import nepgRouter from './nepg';//公众监督员
import nepsRouter from './neps';//网格员
//这里搞个判断对childrenRouter赋值


const globalRoutes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    redirect: { path: '/login' },
    meta: { componentName: 'Home' },
  }, {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { componentName: 'Login' }
  }, {
    path: '/notFound',
    name: 'NotFound',
    component: NotFound,
    meta: { componentName: 'NotFound' }
  }, nepmRouter, nepgRouter, nepsRouter
];



const router = createRouter({
  history: createWebHistory(),
  routes: globalRoutes,

});


router.beforeEach((to: any, from: any, next: any) => {
  let token: string = Cookies.get('token')
  if (!token || !/\S/.test(token)) {
    if (to.path !== 'login') {
      // next('/login')
      next()
    }
    else {
      next()
    }
  } else {
    next()
  }

})

export default router;